var util = require('../utils/util.js');
var app = getApp();
(function() {
    var item = {
        initItem: function(page, list, data, width, renderParam) {
            this.page = page;
            this.list = list;
            this.data = data;
            this.id = data.id
            this.done = (data.state === util.STATE_DONE);

            this.x = 0;
            this.y = 0;
            this.winWidth = width;
            this.width = width;
            this.sliderX = 0;
            this.crossX = 0;
            this.checkX = 0;
            this.checkO = 0;
            this.crossO = 0;
            this.opacity = 1;
            this.display = 'block';
            this.textColor = util.COLOR_WHITE;

            this.renderParam = renderParam;

            this.checkBound = app.checkBound;
            this.crossBound = app.crossBound;
            this.calHeight();
            this.calCrossCheckPos();
        },
        getsIndex: function(param) {
            return this.renderParam + '[' + this.index + '].' + param;
        },
        render: function(param, data) {
            var sIndex = this.getsIndex(param);
            var renderData = this.page.getRenderData();
            renderData[sIndex] = data;
        },
        show: function() {
            this.display = 'block';
            this.render('display', this.display);
        },
        hide: function() {
            this.display = 'none';
            this.render('display', this.display);
        },
        calHeight: function() {
            var fontH = util.rpx2px(app.fontHeight, this.winWidth);
            var paddingH = util.rpx2px(56, this.winWidth) - 2;

            var row = this.data.lineCount;
            this.height = fontH * row + app.rowGap * (row - 1) + paddingH + app.extPadding;
        },
        calCrossCheckPos: function() {
            if (app.ITEM_HEIGHT <= 124) {
                this.imgSize = app.ITEM_HEIGHT;
                this.imgTop = Math.floor((this.height - app.ITEM_HEIGHT) / 2);
            }
            else {
                this.imgSize = 124;
                this.imgTop = Math.floor((this.height - 124) / 2);
            }
        },
        updateCrossCheckPos: function() {
           if (app.ITEM_HEIGHT <= 124) {
               this.imgSize = app.ITEM_HEIGHT + 1;
               this.imgTop = Math.floor((this.height - app.ITEM_HEIGHT + 1) / 2);
           }
           else {
               this.imgSize = 124;
               this.imgTop = Math.floor((this.height - 124) / 2);
           }
           this.render('imgTop', this.imgTop);
           this.render('imgSize', this.imgSize);
        },
        getId: function() {
            return this.data.id;
        },
        setLineCount: function(lineCount) {
            if (this.data.lineCount === lineCount) {
                return;
            }
            this.data.lineCount = lineCount;
            var height = this.height;
            this.calHeight();
            this.setHeight(this.height);
            this.list.onItemHeightChange(this.getOrder(), this.height - height);
            this.updateCrossCheckPos();
        },
        setTextColor: function(textColor) {
            this.textColor = textColor;
            this.render('textColor', textColor);
        },
        setText: function(text, lineCount) {
            this.data.text = text;
            this.render('text', text);
            this.setLineCount(lineCount);
        },
        getText: function() {
            return this.data.text;
        },
        setHeight: function(height) {
            this.height = height;
            this.render('height', height);
        },
        getHeight: function() {
            return this.height;
        },
        setOrder: function(order) {
            this.data.order = order;
        },
        getOrder: function() {
            return this.data.order;
        },
        plusOrder: function() {
            this.data.order++;
        },
        minusOrder: function() {
            this.data.order--;
        },
        setOpacity: function(o) {
            this.opacity = o;
            var animation = wx.createAnimation({duration:300, timingFunction:'ease-in-out'});
            animation.translateY(this.y).opacity(o).step();
            this.render('animation', animation.export());
        },
        setZIndex: function(z_index) {
            this.z_index = z_index;
            this.render('z_index', z_index);
        },
        setLineStyle: function(lineStyle) {
            this.lineStyle = lineStyle;
            this.render('textDecoration', lineStyle);
        },
        showText: function() {
            this.render('textDisplay', 'block');
        },
        hideText: function() {
            this.render('textDisplay', 'none');
        },
        locate: function(y) {
            this.x = 0;
            this.y = y;
            this._scale = 1;
            var animation = wx.createAnimation({duration:300, timingFunction:'ease-in-out'});
            animation.translate3d(this.x, y, 0).step();
            this.render('animation', animation.export());
        },
        lineMove: function(x, y) {
            this.x = x;
            this.y = y;
            var animation = wx.createAnimation({duration:300});
            animation.translate3d(x, y, 0).step();
            this.render('animation', animation.export());
        },
        moveY: function(y) {
            this.y = y;
            var animation = wx.createAnimation({duration:0});
            animation.translateY(y).step();
            this.render('animation', animation.export());
        },
        smoothMoveY: function(y) {
            this.y = y;
            var animation = wx.createAnimation({duration:300, timingFunction:'ease-in-out'});
            animation.translateY(y).step();
            this.render('animation', animation.export());
        },
        setY: function(y) {
            this.y = y;
        },
        getMoveYData: function() {
            var animation = wx.createAnimation({duration:0});
            animation.translateY(this.y).step();
            this.moveYData = animation.export();
            return this.moveYData;
        },
        changeY: function(y) {
            this.y += y;
            this.moveY(this.y);
        },
        smoothChangeY: function(y) {
            this.y += y;
            this.smoothMoveY(this.y);
        },
        smoothMoveX: function(x) {
            this.x = x;
            var animation = wx.createAnimation({duration:180, timingFunction:'ease-in-out'});
            animation.translate3d(x, this.y, 0).step();
            this.render('animation', animation.export());
        },
        moveX: function(x) {
            this.x = x;
            var animation = wx.createAnimation({duration:0});
            animation.translate3d(x, this.y, 0).step();
            this.render('animation', animation.export());
        },
        moveSliderX: function(x) {
            this.sliderX = x; 
            var animation = wx.createAnimation({duration:0});
            animation.translateX(x).step();
            this.render('sliderAnimation', animation.export());
        },
        smoothMoveSliderX: function(x) {
            this.sliderX = x;
            var animation = wx.createAnimation({duration:180, timingFunction:'ease-in-out'});
            animation.translateX(x).step();
            this.render('sliderAnimation', animation.export());
        },
        moveCheckX: function(x) {
            this.checkX = x;
            var animation = wx.createAnimation({duration:0});
            animation.translateX(x).step();
            this.render('checkAnimation', animation.export());
        }, 
        moveCrossX: function(x) {
            this.crossX = x;
            var animation = wx.createAnimation({duration:0});
            animation.translateX(x).step();
            this.render('crossAnimation', animation.export());
        },
        setCheckOpacity: function(o) {
            this.checkO = o;
            var sIndex = this.getsIndex('checkO');
            var renderData = this.page.getRenderData();
            renderData[sIndex] = o; 
        },
        setCrossOpacity: function(o) {
            this.crossO = o;
            var sIndex = this.getsIndex('crossO');
            var renderData = this.page.getRenderData();
            renderData[sIndex] = o;
        }, 
        updatePosition: function(y, top) {
            if (top) {
                this.setZIndex(999);
                this.page.render();
            }
            this.smoothMoveY(y);
            if (top) {
                var s = this;
                setTimeout(function(){
                    s.setZIndex(0);
                    s.page.render();
                }, 180);
            }
        },
        disappear: function(callback) {
            var item = this;
            item.smoothMoveX(-item.width);
            item.page.render();
            if (callback) {
                setTimeout(function() {
                    callback();
                }, 180);
            }
        },
        goback: function(callback) {
            var item = this;
            item.smoothMoveSliderX(0);
            item.page.render();
            if (callback) {
                setTimeout(function() {
                    callback();
                }, 180);
            }
        },
        goAway: function(callback) {
            var item = this;
            item.smoothMoveY(this.y + 1000);
            setTimeout(function() {
                item.moveX(-item.width);
                item.page.render();
            }, 300);
        },
        ontouchstart: function() {
            this.moveCheckX(0);
            this.moveCrossX(0);
            this.page.render();
        },
        ontouchmove: function(dx) {
            var tx = this.sliderX + dx;
            if (this.noMoveRight && tx > 0) return;
            if (this.noMoveLeft && tx < 0) return;

            if (tx > 0) {
                if (tx < app.checkBound) {
                    var o = tx / app.checkBound;
                    this.setCheckOpacity(o);
                    if (this.checkX !== 0) {
                        this.moveCheckX(0);
                    }
                }
                else {
                    dx /= 3;
                    this.moveCheckX(Math.max(0, (this.sliderX + dx) - app.checkBound));
                    if (this.checkO !== 1) {
                        this.setCheckOpacity(1);
                    }
                }
            }
            else if (tx < 0) {
                if (tx > app.crossBound) {
                    var o = tx / app.crossBound;
                    this.setCrossOpacity(o);
                    if (this.crossX !== 0) {
                        this.moveCrossX(0);
                    }
                }
                else {
                    dx /= 3;
                    this.moveCrossX(Math.min(0,  (this.sliderX + dx) - app.crossBound));
                    if (this.crossO !== 1) {
                        this.setCrossOpacity(1);
                    }
                }
            }
            this.moveSliderX(this.sliderX + dx);
            this.page.render();
        },

        ontouchend: function() {
            var item = this
            if (item.sliderX < app.crossBound) {
                item.del();
                return;
            }
            if (item.sliderX > app.checkBound) {
                item.goback(function() {
                    item.complete();
                })
                return;
            }

            item.goback();
        },

        sortstart: function() {
            var sortItem = this.list.sortItem;
            this.setSortItemStyle();
            sortItem.move(0, this.list.y + this.y);
            sortItem.show();
            this.hide();
            this.page.render();
            var s = this;
            setTimeout(function() {
                sortItem.sortstart();
                s.page.render();
            }, 50);
           
        },
        sortmove: function(e, input) {
            var sortItem = this.list.sortItem;
            sortItem.move(sortItem.x + input.deltaX, sortItem.y + input.deltaY);
            this.page.render();

            var c = this.list,
                cy = c.y,
                sy = sortItem.y,
                sbottom = sy + this.height,
                winHeight = app.system.windowHeight;
                
            if (cy < c.downBound &&  sy < app.UP_SORTMOVE_THRESHOLD && input.deltaY < 3) {
                if (!c.sortMoving) {
                    c.sortmove(1, this);
                }
            }
            else if (cy > c.upBound && (winHeight - sbottom) < app.DOWN_SORTMOVE_THRESHOLD && input.deltaY > -3) {
                if (!c.sortMoving) {
                    c.sortmove(-1, this);
                }
            }
            else {
                c.sortMoving = false;
                this.list.checkSwap(this);
            }


        },
        sortend: function(e, speedY) {
            if (typeof this.onsortend === 'function') {
                this.onsortend();
            }
            var sortItem = this.list.sortItem;
            var y = this.list.calUpHeight(this.getOrder());
            this.moveY(y);
            var s = this;
            sortItem.sortend(0, this.list.y + y, function() {
                sortItem.hide();
                s.list.updateColor();
                s.show();
                s.page.render();
            });

            this.page.render();
            this.list.resortData();

            this.list.save();
        },
    };

    function createItem() {
        return Object.create(item);
    }

    module.exports = {createItem: createItem};

})();
